package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.ListIterator;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.resource.sensordata.jaxb.SensorDataIndex;
import org.wattdepot.resource.sensordata.jaxb.SensorDataRef;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Prints information about a single sensordata source at the given day or Prints a table listing
 * all of the sensor data instances for the specified day.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class WattDepotListDailySensordataCommand extends WattDepotCommand {
  /**
   * Prints information about a single sensordata source at the given day.
   * 
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string of daily sensor data.
   */
  public String doCommand(String[] args) {

    if (args.length != 4 || 0 != args[2].compareTo("day")) {
      return "dailysensordata {source} day {day}";
    }
    return getSensorData(args);
  }

  /**
   * Prints information about a single sensordata source at the given day.
   * 
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string named output of daily sensor data.
   */
  private String getSensorData(String[] args) {
    boolean error = false;
    ArrayList<String> outputArray = new ArrayList<String>();
    String output = "";
    XMLGregorianCalendar tstamp = null;
    String strTstamp = "";
    try {
       tstamp = Tstamp.makeTimestamp(args[3]);
       strTstamp = tstamp.toString();
    }
    catch (Exception e) { 
      return "Day is not properly formatted.";
    }
    
    try {
      SensorDataIndex sdIndex = getClient().getSensorDataIndex(args[1]);
      ArrayList<SensorDataRef> sdRef = (ArrayList<SensorDataRef>) sdIndex.getSensorDataRef();
      ListIterator<SensorDataRef> li = sdRef.listIterator();
      while (li.hasNext()) {
        SensorDataRef dataRef = li.next();
        if (dataRef.getTimestamp().toString().startsWith(strTstamp)) {

          outputArray.add(dataRef.getTimestamp().toString() + " ");

          outputArray.add("Tool: ");
          try {

            outputArray.add(dataRef.getTool() + " ");
          }
          catch (Exception e) {
            error = true;
          }
          outputArray.add("Properties: ");

          String[] sSource = dataRef.getSource().split("/");
          SensorData sensorData =
              getClient().getSensorData(sSource[sSource.length - 1], dataRef.getTimestamp());

          outputArray.add(getPropertiesList(sensorData) + cr);

        }
      }
    }
    catch (MiscClientException mce) {
      return CON_NOT_FOUND;
    }
    catch (NotAuthorizedException nae) {
      error = true;
      if (error) {
        return "Application Error.";
      }
    }
    catch (ResourceNotFoundException rne) {
      return RES_NOT_FOUND;
    }
    catch (BadXmlException bxe) {
      error = true;
    }
    output += outputArray.toString();
    output = output.replace("[", "");
    output = output.replace("]", "");
    return output;

  }
}
